package fr.inria.arles.temperatureManagement.utility;

import java.io.FileInputStream;
import java.util.Properties;

/**
 * A Utility Class providing common methods used throughout the application.
 */
public class Utility {

    /** The String SEPERATOR. */
    private static final String SEPERATOR = "_";

    /**
     * Encodes a String array into one String.
     *
     * @param args
     *            the string array to be encoded
     * @return the encoded string
     */
    public static String encode(String... args) {
        int i;
        StringBuilder str = new StringBuilder();
        for (i = 0; i < args.length - 1; i++) {
            str.append(args[i]);
            str.append(SEPERATOR);
        }
        str.append(args[i] + "");
        return str.toString();
    }

    /**
     * Decodes an encoded String to a String array
     *
     * @param str
     *            the string to be decoded
     * @return the decoded string array
     */
    public static String[] decode(String str) {
        return str.split(SEPERATOR);
    }

    /**
     * Loads properties from the specified
     *
     * @param properties
     *            the properties object to be populated.
     *@param filename
     *             the file containing the properties.
     */
    public static void loadProperties(Properties properties, String filename) {
        try {
            properties.load(new FileInputStream(filename));
            System.out.println("Propeties loaded from file:" + filename);
        } catch (Exception e) {
            System.out.println("Unable to load properties from file:"
                    + filename);
        }
    }
}
